package com.itheima.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/servletDemo01")
public class ServletDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取虚拟路径
        String contextPath = req.getContextPath();
        System.out.println(contextPath);

        //获取映射路径
        String servletPath = req.getServletPath();
        System.out.println(servletPath);

        //3.获取访问者ip
        String remoteAddr = req.getRemoteAddr();
        System.out.println(remoteAddr);

        //获取指定的头信息
        String referer = req.getHeader("Referer");
        System.out.println(referer);

        Enumeration<String> headers = req.getHeaders("User-Agent");
        while (headers.hasMoreElements()) {
            String name = headers.nextElement();
            System.out.print(name+"  ");
        }
        //请求转发
        //设置共享数据
        req.setAttribute("encoding","gbk");
        RequestDispatcher rd = req.getRequestDispatcher("/servletDemo02");
        rd.forward(req,resp);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
